import java.sql.DriverManager

import org.mongodb.scala.{Completed, Document, MongoClient, MongoCollection, MongoDatabase, Observer}

import scala.collection.mutable

object Mysql2Mongo {
  def main(args: Array[String]): Unit = {
    Class.forName("com.mysql.jdbc.Driver")
    val mysqlConn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hs_data?serverTimezone=UTC ", "root", "dante123")
    val stmt = mysqlConn.createStatement()
    val rs = stmt.executeQuery("select * from sp_ykt_xflsjlb where xh is not null")

    val mongoDB : MongoDatabase = MongoClient().getDatabase("local")
    val mongoCollection : MongoCollection[Document] = mongoDB.getCollection("sp_ykt_xflsjlb")

    while(rs.next()){
      mongoCollection.insertOne(Document(
        "xh" -> rs.getString("xh"),
        "xm" -> rs.getString("ckrxm"),
        "shbh" -> rs.getString("shbh"),
        "shmc" -> rs.getString("shmc"),
        "xfsj" -> rs.getString("xfsj"),
        "xfje" -> rs.getDouble("xfje"),
        "czlxmc" -> rs.getString("czlxmc"),
        "kye" -> rs.getDouble("kye")
      ))subscribe(new Observer[Completed] {

        override def onNext(result: Completed): Unit = println("Inserted")

        override def onError(e: Throwable): Unit = println("Failed")

        override def onComplete(): Unit = println("Completed")
      })
    }

  }
}
